import numpy as np
import matplotlib.pyplot as plt

"""
解析mAP表格文件
"""
"""
Evaluate per-class mAP50:
|  aeroplane  |  bicycle  |  boat  |  bottle  |  car   |  cat   |  chair  |  diningtable  |  dog   |  horse  |  person  |  pottedplant  |  sheep  |  train  |  tvmonitor  |  bird  |  bus   |  cow   |  motorbike  |  sofa  |
|:-----------:|:---------:|:------:|:--------:|:------:|:------:|:-------:|:-------------:|:------:|:-------:|:--------:|:-------------:|:-------:|:-------:|:-----------:|:------:|:------:|:------:|:-----------:|:------:|
|   87.467    |  86.479   | 68.791 |  73.222  | 86.376 | 87.241 | 67.070  |    73.611     | 84.856 | 83.795  |  86.082  |    54.035     | 81.000  | 85.400  |   82.391    | 19.091 | 68.148 | 48.105 |   57.868    | 3.927  |
[06/15 19:23:05 fsdet.evaluation.pascal_voc_evaluation]: Evaluate overall bbox:
|   AP   |  AP50  |  AP75  |  bAP   |  bAP50  |  bAP75  |  nAP   |  nAP50  |  nAP75  |
|:------:|:------:|:------:|:------:|:-------:|:-------:|:------:|:-------:|:-------:|
| 43.725 | 69.248 | 47.508 | 50.688 | 79.188  | 55.422  | 22.836 | 39.428  | 23.766  |
"""

def get_aps(filename):
    with open(filename) as f:
        lines = f.readlines()

    class_ap = lines[3]
    statistic_ap = lines[-1]

    class_ap = class_ap[2:].strip().split("|")[:-1]
    ap_per_class = [i.strip() for i in class_ap]
    ap_per_class = [float(i) for i in ap_per_class]

    statistic_ap = statistic_ap[2:].strip().split("|")[:-1]
    s_ap = [i.strip() for i in statistic_ap]
    s_ap = [float(i) for i in s_ap]
    return ap_per_class, s_ap


if __name__ == '__main__':
    class_label = ['aeroplane', 'bicycle', 'boat', 'bottle', 'car', 'cat', 'chair', 'diningtable', 'dog', 'horse',
                   'person', 'pottedplant', 'sheep', 'train', 'tvmonitor', 'bird', 'bus', 'cow', 'motorbike', 'sofa']

    statistic_label = ['AP', 'AP50', 'AP75', 'bAP', 'bAP50', 'bAP75', 'nAP', 'nAP50', 'nAP75']

    aps_1 = get_aps("./aps/baseline_0615_nap_22.836.txt")
    aps_2 = get_aps("./aps/autoencoder_0615_1feat_finetune_0.001.txt")
    aps_3 = get_aps("./aps/autoencoder_0615_1feat_finetune_0.0001.txt")

    x = np.array(list(range(len(aps_1[0]))))
    total_width, n = 0.8, 3
    width = total_width / n
    x = x - (total_width - width) / 2

    plt.bar(x, aps_1[0], width=width, label="baseline")
    plt.bar(x + width, aps_2[0], width, label="0.001")
    plt.bar(x + 2 * width, aps_3[0], width, label="0.0001")
    plt.legend()
    plt.show()
